import InteractionUtil from '../../shell/ui/interaction';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    amount: 0,
    checked: 1,
    items: [{
      label: '企业单位',
      value: 0
    }, {
      label: '个人/非企业单位',
      value: 1
    }],
    invoice: {
      order_id: '',
      invoice_type_id: '',
      invo_cust_name: '',
      invoice_tax_number: '',
      invoice_telephone: '',
      invoice_note: '',
      invoice_electronic_email: ''
    },
    showConfirm: false,
    state: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().getSaleMode()
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptData', (res) => {
      const { amount, data, orderId } = res
      let { invoice } = this.data
      invoice.order_id = orderId
      const { invoice_state, invoice_state_msg, invoice_data } = data
      if (invoice_state === 1) {
        this.setData({
          amount,
          invoice,
          state: invoice_state
        })
        this.chooseInvoice()
      } else {
        const { invoice_type_id } = invoice_data
        this.setData({
          checked: invoice_type_id * 1 === 3 ? 0 : 1,
          invoice: Object.assign(invoice, invoice_data),
          amount,
          state: invoice_state
        })
      }
    })
  },
  chooseInvoice() {
    wx.chooseInvoiceTitle({
      success: res => {
        let { invoice } = this.data
        const { bankAccount,
          bankName,
          companyAddress,
          taxNumber,
          telephone,
          title,
          type
        } = res
        Object.assign(invoice, {
          invoice_type_id: type * 1 === 0 ? 3 : 2,
          invo_cust_name: title,
          invoice_tax_number: taxNumber,
          invoice_telephone: telephone,
          invoice_note:  '',
          invoice_electronic_email: '',
          invoice_company_address:companyAddress,
          invoice_bank_name:bankName,
          invoice_bank_account:bankAccount,

        })
        this.setData({
          checked: type * 1,
          invoice
        })
      },
      fail: _ => {
        wx.navigateBack()
      }
    })
  },
  emailHandler(e) {
    const { value } = e.detail
    let { invoice } = this.data
    invoice.invoice_electronic_email = value
    this.setData({
      invoice
    })
  },
  descInput(e) {
    const { value } = e.detail
    let { invoice } = this.data
    invoice.invoice_note = value
    this.setData({
      invoice
    })
  },
  phoneHandler(e) {
    const { value } = e.detail
    let { invoice } = this.data
    invoice.invoice_telephone = value
    this.setData({
      invoice
    })
  },
  radioChange(e) {
    const {detail} = e
    const {value} = detail
    this.chooseInvoice()
  },
  submitForm() {
    const { invoice } = this.data
    const { invoice_electronic_email, invoice_type_id, invoice_telephone } = invoice
    if (invoice_type_id * 1 === 2) {
      if (!/^1[3456789]\d{9}$/.test(invoice_telephone)) {
        InteractionUtil.showNotice("手机号码输入有误!")
        return
      }
    }
    if (!/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(invoice_electronic_email)) {
      InteractionUtil.showNotice("电子邮箱输入有误!")
      return
    }
    this.setData({
      showConfirm: true
    })
  },
  submitInvoice() {
    const { invoice } = this.data
    wx.$http().request({
      url: '/AppMaster/PosOrder/submitOrderElectronicInvoice',
      data: invoice,
      method: 'POST'
    }).then(_ => {
      const { _data} = _
      const { result } = _data
      if (result * 1 === 0) {
        InteractionUtil.showSuccess('提交成功')
        wx.navigateTo({
          url: '/subPage/invoiceSuc/index',
        })
      }
    })
  },
  closeHandler() {
    this.setData({
      showConfirm: false
    })
  },
  invoiceDetail() {
    const { amount } = this.data
    wx.navigateTo({
      url: '/subPage/invoiceView/index',
      success: res => {
        res.eventChannel.emit('acceptData', { amount })
      }
    })
  }
})